package cz.fel.via.timemanagement;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import cz.cvut.fel.via.kosapirest.Fasade;
import cz.cvut.fel.via.kosapirest.domain.Note;
import cz.cvut.fel.via.kosapirest.domain.Student;

import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ImageView.ScaleType;

public class NoteActivity extends Activity {
	
	LinearLayout placeForNotes;
	private static final int NO_TEXT = 1;
	private static final int SERVER_PROBLEMS = 2;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.note);

		String username = TimeManagement.getUser();
		
		Fasade fasade = TimeManagement.getFasade();
		Student student = fasade.find(username, Student.class);
		
		TextView name = (TextView) findViewById(R.id.studentName);
		name.setText(student.getFirstName()+" "+student.getLastName());
		name.setGravity(Gravity.CENTER_HORIZONTAL);
		
		placeForNotes = (LinearLayout) findViewById(R.id.notes);
		List<Note> notes = TimeManagement.findNotes();
		for (Note n : notes) {
			placeForNotes.addView(noteDisplay(this, n), 0);
		}
		
	}
	
	
	private LinearLayout noteDisplay (final Context cnt, final Note n) {
		final LinearLayout layout = new LinearLayout(cnt);
		layout.setOrientation(LinearLayout.VERTICAL);
		layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
		//layout.setSc
		
		LinearLayout dateTime = new LinearLayout (cnt);
		dateTime.setOrientation(LinearLayout.HORIZONTAL);
		
		dateTime.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
		
		TextView date = new TextView(cnt);
		
		Date dateCreated = n.getCreated();
        SimpleDateFormat dateFormater = new SimpleDateFormat("d.M.yyyy HH:mm");
        char [] dateText = dateFormater.format(dateCreated).toCharArray();
        date.setText(dateText, 0, dateText.length);
        //date.setLayoutParams(new LayoutParams(Gravity.LEFT)); //ma nastavit gravitu; tedy pomer 4 : 1
        date.setTextSize(24);
        date.setTextColor(Color.CYAN);
        date.setPadding(0, 0, 20, 0);
        
        ImageView delete = new ImageView(cnt);
        delete.setOnClickListener(new OnClickListener () {
			public void onClick(View v) {
				TimeManagement.getFasade().deleteNote(n.getId());
				
				//TODO nejaka reakce, odchytnuti vyjimky
				
				placeForNotes.removeView(layout);
				
			}
		});
        delete.setBackgroundResource(R.drawable.delete_64);
        //delete.setLayoutParams(new LayoutParams(Gravity.RIGHT)); //ma nastavit gravitu; tedy pomer 4 : 1
        
        
		ImageView separator = new ImageView(cnt);
        separator.setImageResource(R.drawable.horizontal);
        separator.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT ));
        separator.setScaleType(ScaleType.FIT_XY);
        separator.setPadding(5, 5, 2, 2);
        
		TextView text = new TextView(cnt);
		
		char [] textNote = n.getText().toCharArray();
		
		text.setText(textNote, 0, textNote.length);
		
		dateTime.addView(date);
		dateTime.addView(delete);
		
		layout.addView(dateTime);
		layout.addView(text);
		layout.addView(separator);
		
		return layout;
	}
	
	
	public void addNote(View v) {

		String text = ((EditText) findViewById(R.id.noteText)).getText()
				.toString().trim();
		if (text.equals("")) {
			showDialog(NO_TEXT);
			return;
		}
		Note note = new Note(null, Calendar.getInstance().getTime(), text, TimeManagement.getUser());
		
		Fasade fasade = TimeManagement.getFasade();
		note = fasade.saveOrUpdateNote(note);

		placeForNotes.addView(noteDisplay(this, note), 0);
		((EditText) findViewById(R.id.noteText)).setText("");
	}
	


	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case NO_TEXT:
			AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
			alertbox.setMessage(R.string.noText);
			alertbox.setNeutralButton("Ok", null);
			return alertbox.show();

		case SERVER_PROBLEMS:
			AlertDialog.Builder alertbox1 = new AlertDialog.Builder(this);
			alertbox1.setMessage(R.string.communicationProblem);
			alertbox1.setNeutralButton("Ok", null);
			return alertbox1.show();
		}
		return null;
	}
	
	public void student(View v) {
		Intent intent = new Intent(this, StudentActivity.class);
		startActivity(intent);
	}
	
	public void exams(View v) {
		Intent intent = new Intent(this, ExamActivity.class);
		startActivity(intent);
	}
	
	public void schedule(View v) {
		Intent intent = new Intent(this, ScheduleActivity.class);
		startActivity(intent);
	}
}
